package com.wmx.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;

import java.util.concurrent.TimeUnit;

/**********************************
 * @author wmx
 * @date 2019/9/25 10:06
 * @description:
 * @version 1.0
 ************************************/
@Slf4j
@Configuration
public class EventListenerConfig {
    /*@Async
    @EventListener
    public void handleEvent(Object event) {
        //监听所有事件 可以看看 系统各类时间 发布了哪些事件
        //可根据 instanceof 监听想要监听的事件
        if(event instanceof CustomEvent) {

        }
        log.info("监听所有的事件：{}", event);
    }*/
    @Async
    @EventListener
    public void handleCustomEvent(CustomEvent customEvent) {
        try {
            TimeUnit.SECONDS.sleep(30);
            //监听 CustomEvent事件
            log.info("监听到CustomEvent事件，消息为：{}, 发布时间：{}", customEvent.getMessageEntity(), customEvent.getTimestamp());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 监听 code为oKong的事件
     */
    @Async
    @EventListener(condition="#customEvent.messageEntity.code == 'oKong'")
    public void handleCustomEventByCondition(CustomEvent customEvent) {
        //监听 CustomEvent事件
        log.info("监听到code为'oKong'的CustomEvent事件，消息为：{}, 发布时间：{}", customEvent.getMessageEntity(), customEvent.getTimestamp());
    }


}
